package com.polaris.live.popup

import android.content.Context
import androidx.lifecycle.LifecycleOwner
import com.lxj.xpopup.core.CenterPopupView
import com.polaris.live.R
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.databinding.PopupPublicTipsBinding
import com.polaris.live.utils.XPopupUtils

/**
 * 公共确认弹窗
 * @author Created by 天晴 on 2024/3/21 20:38
 * @since 1.0.0
 **/
class PublicTipsPopup(
    context: Context,
    private val title: String,
    private val message: String,
    private val topMessage: String,
    private val bottomMessage: String,
    private val onTop: (() -> Unit)? = null,
    private val onBottom: (() -> Unit)? = null,
    private val showTop: Boolean,
) : CenterPopupView(context) {

    private lateinit var mBinding: PopupPublicTipsBinding
    override fun getImplLayoutId(): Int {
        return R.layout.popup_public_tips
    }

    override fun onCreate() {
        super.onCreate()
        mBinding = PopupPublicTipsBinding.bind(popupImplView)
        mBinding.title.text = title
        mBinding.message.text = message
        mBinding.tvTop.text = topMessage
        mBinding.tvBottom.text = bottomMessage
        mBinding.tvTop.setOnSingleClickListener {
            dismiss()
            onTop?.invoke()
        }
        mBinding.tvBottom.setOnSingleClickListener {
            dismiss()
            onBottom?.invoke()
        }

        mBinding.tvTop.visibility = if (showTop) VISIBLE else GONE
    }

    companion object {

        fun show(
            context: Context,
            owner: LifecycleOwner?,
            title: String,
            message: String,
            topMessage: String,
            bottomMessage: String,
            showTop: Boolean = true,
            isViewMode: Boolean = true,
            onTop: (() -> Unit)? = null,
            onBottom: (() -> Unit)? = null,
        ) {
            XPopupUtils.showViewPopup(
                PublicTipsPopup(context, title, message, topMessage, bottomMessage, onTop, onBottom, showTop),
                {
                    hasShadowBg(true)
                    isViewMode(isViewMode)
                },
                lifecycle = owner?.lifecycle
            )
        }
    }
}